<template>
	<view class="login" :style="{height:windowHeight + 'px'}">
		<view class="content">
			<!-- 头部logo -->
			<view class="header">
				<image :src="globalConfig.logo || '/static/images/missing-face.png'"></image>
			</view>
			<!-- 主体表单 -->
			<view class="main">
				<wInput
					v-model="phoneData"
					type="text"
					maxlength="11"
					placeholder="手机号码"
				></wInput>
				<wInput
					v-model="passData"
					type="password"
					placeholder="密码"
				></wInput>
			</view>
			<wButton 
				class="wbutton"
				text="登 录"
				:bgColor="btnColor"
				:rotate="isRotate" 
				@click="startLogin"
			></wButton>
			<!-- 底部信息 -->
			<view class="footer">
				<text @click="navTo('/pages/public/forget')">找回密码</text>
				<text>|</text>
				<text @click="navTo('/pages/public/register')">注册账号</text>
			</view>
			<!-- #ifndef H5 -->
			<view class="app-login">
				<text class="login_other">第三方授权登录</text>
			</view>
			<view class="other_login cuIcon">
				<!-- #ifdef MP-WEIXIN -->
				<button class="login_icon" @getuserinfo="wxLogin" open-type="getUserInfo" withCredentials="true">
					<view class="cuIcon-weixin"></view>
				</button>
				<!-- #endif -->
			</view>
			<!-- #endif -->
		</view>
	</view>
</template>

<script>
	var _this;
	import base from '@/common/base.js';
	import wInput from '@/components/watch-login/watch-input.vue' //input
	import wButton from '@/components/watch-login/watch-button.vue' //button
	
	export default {
		mixins: [base],
		data() {
			return {
				windowHeight: 500,
				phoneData:'', //用户/电话
				passData:'', //密码
				isRotate: false, //是否加载旋转
				btnColor: 'rgb(5, 187, 29)'
			};
		},
		created() {
			let systemInfo = uni.getSystemInfoSync();
			this.windowHeight = systemInfo.windowHeight + systemInfo.statusBarHeight;
		},
		components:{
			wInput,
			wButton,
		},
		methods: {
			startLogin() {
				if(this.isRotate){
					return false;
				}
				var phone_reg = /^[1][3,4,5,7,8,9][0-9]{9}$/;
				if (!phone_reg.test(this.phoneData)) {
					this.$config.toast("手机号不正确，请重新输入");
					return;
				};
				if (this.passData.length == 0) {
					this.$config.toast("请输入密码");
					return;
				}
				let data = {
					mobile: this.phoneData,
					password: this.passData
				};
				uni.showLoading({
					title: "登录中..."
				});
				this.isRotate = true;
				this.$http.post('api/passport/login', data).then((res) => {
					this.isRotate = false;
					uni.hideLoading();
					this.login(res);
					// #ifdef MP-WEIXIN||MP-QQ
					this.resetConfig(res.member);
					// #endif
					this.$emit('login');
				}).catch((error) => {
					this.isRotate = false;
					uni.hideLoading();
					this.$config.toast(error.message);
				});
			},
			wxLogin() {
				uni.login({
					provider: 'weixin',
					success: (res) => {
						uni.getUserInfo({
							success: (infoRes) => {
								let data = {
									code: res.code,
									signature: infoRes.signature,
									encryptedData: infoRes.encryptedData,
									rawData: infoRes.rawData,
									iv: infoRes.iv,
									parent_id: this.$config.getParentId()
								};
								uni.showLoading({
									title: "登录中..."
								});
								this.$http.post('api/passport/wechat-mp', data).then((res) => {
									uni.hideLoading();
									this.login(res);
									// #ifdef MP-WEIXIN||MP-QQ
									this.resetConfig(res.member);
									// #endif
									this.$emit('login');
								}).catch((error) => {
									uni.hideLoading();
									this.$config.toast(error.message);
								});
							},
							fail: () => {
								//获取不到信息
							}
						});
					},
					fail: (err) => {
			
					}
				});
			},
			resetConfig(userInfo){
				if (userInfo.account.user_money > 0 && this.globalConfig.wechat_mod == 0) {
					let config = this.globalConfig;
					config.wechat_mod = 1;
					this.setGlobalConfig(config);
					this.setReloadIndex(true);
				}
			},
			navTo(url) {
				uni.navigateTo({
					url: url
				});
			}
		}
	}
</script>

<style>
	@import url("@/components/watch-login/css/icon.css");
	@import url("@/static/style/login.css");
	.login {
		background: #FFFFFF;
	}
	
	.other_login button{
		border-radius: 0;
		padding: 0;
		margin: 0;
		line-height:inherit;
		background-color: #FFFFFF;
	}
	
	.other_login button::after {
		border: none;
	}
</style>
